package patrol.mobile.com.mobilepatrol.activity.security.emergency;

import android.app.Activity;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.BaseActivity;
import patrol.mobile.com.mobilepatrol.activity.security.ConsTitles;
import patrol.mobile.com.mobilepatrol.activity.security.StickyEventJiDi;
import patrol.mobile.com.mobilepatrol.api.Urls;
import patrol.mobile.com.mobilepatrol.bean.Ensure1Bean;
import patrol.mobile.com.mobilepatrol.bean.Ensure2Bean;
import patrol.mobile.com.mobilepatrol.bean.MaterialBean;
import patrol.mobile.com.mobilepatrol.utils.CommonUtil;
import patrol.mobile.com.mobilepatrol.utils.GsonUtil;
import patrol.mobile.com.mobilepatrol.utils.net.HttpCallBack;
import patrol.mobile.com.mobilepatrol.utils.net.HttpSender;

/**
 * Created by fuzhen.li on 2018/9/18.
 */
public class EmergencyEnsure2Activity extends BaseActivity {


    @BindView(R.id.ll_wuZi)
    LinearLayout llWuZi;
    @BindView(R.id.ll_zhuangBei)
    LinearLayout llZhuangBei;
    @BindView(R.id.ll_duiWu)
    LinearLayout llDuiWu;
    @BindView(R.id.tv_materialName)
    TextView tvMaterialName;
    @BindView(R.id.tv_unit)
    TextView tvUnit;
    @BindView(R.id.tv_amount)
    TextView tvAmount;
    @BindView(R.id.ll_visible_wuZi)
    LinearLayout llVisibleWuZi;
    @BindView(R.id.tv_equipName)
    TextView tvEquipName;
    @BindView(R.id.tv_zb_unit)
    TextView tvZbUnit;
    @BindView(R.id.tv_zb_amount)
    TextView tvZbAmount;
    @BindView(R.id.ll_visible_zhuangBei)
    LinearLayout llVisibleZhuangBei;
    @BindView(R.id.tv_du_ranksName)
    TextView tvDuRanksName;
    @BindView(R.id.tv_du_ranksAmount)
    TextView tvDuRanksAmount;
    @BindView(R.id.tv_ranksUnit)
    TextView tvRanksUnit;
    @BindView(R.id.tv_ranksHeader)
    TextView tvRanksHeader;
    @BindView(R.id.tv_ranksPost)
    TextView tvRanksPost;
    @BindView(R.id.tv_ranksContact)
    TextView tvRanksContact;
    @BindView(R.id.ll_visible_duiWu)
    LinearLayout llVisibleDuiWu;

    @BindView(R.id.tv_wuzi)
    TextView tvWuZi;

    @BindView(R.id.tv_zhuangbei)
    TextView tvZhuangBei;

    @BindView(R.id.tv_duiwu)
    TextView tvDuiWu;


    @BindView(R.id.tab_layout_other_business)
    TabLayout tabLayoutOtherBusiness;
    @BindView(R.id.vp_other_business)
    ViewPager vpOtherBusiness;
    private List<Ensure2Bean.AbkSupportBaseMaterialListBean> abkSupportBaseMaterialListBeanList = new ArrayList<>();
    private List<Ensure2Bean.AbkSupportBaseEquipListBean> abkSupportBaseEquipListBeanList = new ArrayList<>();
    private List<Ensure2Bean.AbkSupportBaseRanksListBean> abkSupportBaseRanksListBeanList = new ArrayList<>();

    private Fragment[] tabs;



    private void switchTab(int index) {
        if (vpOtherBusiness != null) {
            vpOtherBusiness.setCurrentItem(index, false);
        }
    }

    @Override
    public void initData() {
        super.initData();

        TabFragmentAdapter adapter = new TabFragmentAdapter(getSupportFragmentManager(), this);
        vpOtherBusiness.setOffscreenPageLimit(3);
        vpOtherBusiness.setAdapter(adapter);
        tabLayoutOtherBusiness.setupWithViewPager(vpOtherBusiness);
        switchTab(0);
    }

    /*
     * 此结构为activity+fragment+fragment
     */
    class TabFragmentAdapter extends FragmentStatePagerAdapter {
        private String[] TITLES;



        private TabFragmentAdapter(FragmentManager fm, Activity context) {
            super(fm);
            TITLES = new String[]{"应急物资","应急装备","应急队伍"};
            tabs = new Fragment[TITLES.length];
            tabs[0] = new WuziFragment();
            tabs[1] = new ZhuangBeiFragment();
            tabs[2] = new DuiWuFragment();
        }

        @Override
        public Fragment getItem(int position) {
            return tabs[position];
        }

        @Override
        public int getCount() {
            return TITLES.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return TITLES[position];
        }
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_emergency_ensure2;
    }

    @Override
    public void setListener() {
        llWuZi.setBackgroundColor(getResources().getColor(R.color.gray));
        llZhuangBei.setBackgroundColor(getResources().getColor(R.color.transparent));
        llDuiWu.setBackgroundColor(getResources().getColor(R.color.transparent));
        String id = getIntent().getStringExtra("id");
        requestData(id);
    }

    @Override
    public void initTitle() {
        iv_back.setImageResource(R.mipmap.up_white);
        iv_back.setVisibility(View.VISIBLE);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CommonUtil.exitActivityAndBackAnim(EmergencyEnsure2Activity.this, true);
            }
        });
        tv_center_title.setVisibility(View.VISIBLE);
        tv_center_title.setText(ConsTitles.EMERGENCY_ENSURE_2);
    }

    @OnClick({R.id.ll_wuZi, R.id.ll_zhuangBei, R.id.ll_duiWu})
    public void onTabEnsureClick(View v) {

        switch (v.getId()) {

            case R.id.ll_wuZi:

                llWuZi.setBackgroundColor(getResources().getColor(R.color.gray));
                llZhuangBei.setBackgroundColor(getResources().getColor(R.color.transparent));
                llDuiWu.setBackgroundColor(getResources().getColor(R.color.transparent));


                llVisibleWuZi.setVisibility(View.VISIBLE);
                llVisibleZhuangBei.setVisibility(View.GONE);
                llVisibleDuiWu.setVisibility(View.GONE);

                break;
            case R.id.ll_zhuangBei:
                llWuZi.setBackgroundColor(getResources().getColor(R.color.transparent));
                llZhuangBei.setBackgroundColor(getResources().getColor(R.color.gray));
                llDuiWu.setBackgroundColor(getResources().getColor(R.color.transparent));

                llVisibleWuZi.setVisibility(View.GONE);
                llVisibleZhuangBei.setVisibility(View.VISIBLE);
                llVisibleDuiWu.setVisibility(View.GONE);

                break;
            case R.id.ll_duiWu:
                llWuZi.setBackgroundColor(getResources().getColor(R.color.transparent));
                llZhuangBei.setBackgroundColor(getResources().getColor(R.color.transparent));
                llDuiWu.setBackgroundColor(getResources().getColor(R.color.gray));

                llVisibleWuZi.setVisibility(View.GONE);
                llVisibleZhuangBei.setVisibility(View.GONE);
                llVisibleDuiWu.setVisibility(View.VISIBLE);

                break;
        }
    }


    private void requestData(String id) {
        HttpSender sender = new HttpSender(this, baseUrl, Urls.EMERGENCY_ENSURE_2_LIST);
        HashMap<String, String> map = new HashMap<>();
        map.put("id", id);
        sender.requestPost(map, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            //MaterialBean    EquipBean    RanksBean
            @Override
            public void onResponse(String code, String response) {

                Ensure2Bean ensure2BeanList = GsonUtil.GsonToBean(response, Ensure2Bean.class);

                EventBus.getDefault().postSticky(new StickyEventJiDi(ensure2BeanList));

                abkSupportBaseMaterialListBeanList = ensure2BeanList.getAbkSupportBaseMaterialList();
                abkSupportBaseEquipListBeanList = ensure2BeanList.getAbkSupportBaseEquipList();
                abkSupportBaseRanksListBeanList = ensure2BeanList.getAbkSupportBaseRanksList();


                if (abkSupportBaseMaterialListBeanList != null && abkSupportBaseMaterialListBeanList.size() > 0) {
                    tvMaterialName.setText(abkSupportBaseMaterialListBeanList.get(0).getMaterialName());
                    tvUnit.setText(abkSupportBaseMaterialListBeanList.get(0).getUnit());
                    tvAmount.setText(abkSupportBaseMaterialListBeanList.get(0).getAmount());


                    tvEquipName.setText(abkSupportBaseEquipListBeanList.get(0).getEquipName());
                    tvZbUnit.setText(abkSupportBaseEquipListBeanList.get(0).getUnit());
                    tvZbAmount.setText(abkSupportBaseEquipListBeanList.get(0).getAmount());


                    tvDuRanksName.setText(abkSupportBaseRanksListBeanList.get(0).getRanksName());
                    tvDuRanksAmount.setText(abkSupportBaseRanksListBeanList.get(0).getRanksAmount());
                    tvRanksUnit.setText(abkSupportBaseRanksListBeanList.get(0).getRanksUnit());
                    tvRanksHeader.setText(abkSupportBaseRanksListBeanList.get(0).getRanksHeader());
                    tvRanksPost.setText(abkSupportBaseRanksListBeanList.get(0).getRanksPost());
                    tvRanksContact.setText(abkSupportBaseRanksListBeanList.get(0).getRanksContact());
                }


            }
        });
    }


}
